package com.example.tms.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.tms.entity.EmployeeDepartment;

public interface EmployeeDepartmentMapper extends BaseMapper<EmployeeDepartment> {
    
    @Select("SELECT * FROM employee_department WHERE emp_id = #{empId} AND is_current = 1 AND is_deleted = 0")
    EmployeeDepartment findCurrentDepartmentByEmployeeId(Integer empId);
    
    @Select("SELECT * FROM employee_department WHERE emp_id = #{empId} AND state = '已通过' AND is_deleted = 0")
    List<EmployeeDepartment> selectByEmpId(@Param("empId") Integer empId);

    @Select("SELECT * FROM employee_department WHERE dep_id = #{depId} AND state = '已通过' AND is_deleted = 0") 
    List<EmployeeDepartment> selectByDepId(@Param("depId") Integer depId);

    @Select("SELECT * FROM employee_department WHERE emp_id = #{empId} AND is_current = 1 AND state = '已通过' AND is_deleted = 0")
    EmployeeDepartment selectCurrentByEmpId(@Param("empId") Integer empId);

    @Select("<script>" +
            "SELECT * FROM employee_department " +
            "WHERE emp_id IN <foreach item='id' collection='empIds' open='(' separator=',' close=')'>#{id}</foreach> " +
            "AND is_current = 1 AND state = '已通过' AND is_deleted = 0" +
            "</script>")
    List<EmployeeDepartment> findCurrentDepartmentsByEmployeeIds(@Param("empIds") List<Integer> empIds);

    @Select("SELECT * FROM employee_department WHERE state = '待审批' AND is_current = 0 AND is_deleted = 0")
    List<EmployeeDepartment> selectPendingTransfers();

    @Select("SELECT * FROM employee_department WHERE creator_id = #{creatorId} AND is_deleted = 0")
    List<EmployeeDepartment> selectTransfersByCreator(@Param("creatorId") Integer creatorId);
}
